/**
 * Created by RickyTT on 14-1-25.
 */
var db1Obj = require('./db');
var iMysql = require('./sqldb');
var UserInfo = require('./userInfo.js');
var async = require('async');

function User(user) {
    this.name = user.name;
    this.password = user.password;
    this.createTime = user.createTime;
    this.id = user.id;
    this.updateTime = user.updateTime;
};


module.exports = User;

User.getById = function getById(id,callback){
    db1Obj.sql('select * from user where id = ?',id,function(err,result){
        if(err){
            callback(err,result);
            //console.log(err);
        }else{
//            console.log(result);
            if(result.length > 0){
                var user = new User(result[0]);
                callback(err,user);
            }else{
                callback(err,result);
            };
        };
    });
};


User.getByName = function getByName(name,callback){
    db1Obj.sql('select * from user where name = ?',name,function(err,result){
        if(err){
            console.log(err);
        }else{
            if(result.length > 0){
                var user = new User(result[0]);
                callback(err,user);
            }else{
                callback(err,result);
            };
        };
    });
};


User.getByNameAndPassword = function getByNameAndPassword(name,password,callback){
    db1Obj.sql('select * from user where name = "'+name+'" and password = "'+password+'"',function(err,result){
        if(err){
            callback(err,null);
        }else{
            if(result.length > 0){
                var user = new User(result[0]);
                callback(err,user);
            }else{
                callback(err,result);
            };
        };
    });
};



User.getAll = function getAll(callback){
    db1Obj.sql('select * from user',function(err,result){
        if(err){
            callback(err,null);
        }else{
            var users = [];
            var dbUsers = result;
            for(var i = 0;i<dbUsers.length;i++){
                users.push(new User(dbUsers[i]));
            }
            callback(err,users);
        }
    });
};

User.insert = function insert(name,password,callback){
    var trans = iMysql.transaction();

    async.auto({
        insertUser:function(cb){
            trans.sql('insert into user (name,password) values ("'+name+'","'+password+'")',function(err,result){
                if(err){
                    callback(err,result);
                    //console.log(err);
                    throw err;
                }else{
                    trans.commit(function(err,result1){
                        if(err){
                            callback(err,null);
                            throw err;
                        }else{
//                            console.log(result);
                            cb(err,result);
                        };
                    });
                };
            });
        },
        insertUserInfo:['insertUser',function(cb1,result1){
            console.log(result1.insertUser.insertId);
            if(result1.insertUser)
            {
                UserInfo.insert(result1.insertUser.insertId,1,null,0,null,function(err,result2){
                    cb1(err,result2);
                });
            };
        }]
    },function(err,result){
        callback(err,result.insertUser);
    });

    /*var userId;
    trans.sql('insert into user (name,password) values ("'+name+'","'+password+'")',function(err,result){
        if(err){
            callback(err,result);
            //console.log(err);
            throw err;
        }else{


            trans.commit(function(err,result1){
                if(err){
                    callback(err,null);
                    throw err;
                }else{
                    console.log(result);
                    userId = result.insertId;
                    UserInfo.insert(userId,1,null,0,null,function(err,result){
                        console.log('userInfo err '+err);
                        console.log('userInfo result '+result);
                    });
                    callback(1,result);
//                    console.log(result);
                };
            });
        };
    });*/


};

User.update = function update(password,id,callback){
    var trans = iMysql.transaction();
    trans.sql('update user set password = "'+password+'" where id = '+id+'',function(err){
        if(err){
            callback(err);
        }else{
            trans.commit(function(err){
                if(err){
                    callback(err);
                    console.log(err);
                }else{
                    callback(1);
                    console.log(arguments);
                };
            });
        };
    });
};


User.deleteById = function deleteById(id,callback){
    var trans = iMysql.transaction();

    async.parallel([
        function(cb){
            trans.sql('delete from user where id = '+id+'',function(err,result){
                if(err){
                    cb(err,result);
//                    console.log(err);
                }else{
                    trans.commit(function(err){
                        if (err) {
                            cb(err,result);
                            throw err;
                        } else {
                            cb(err,result);
//                            console.log(err);
                        };
                    });
                };
            });
        },

        function(cb){
            UserInfo.deleteByUserId(id,function(err,result){
                cb(err,result);
            })
        }
    ],function(err,result){
        console.log(err);
        callback(err,result[0]);
    })

};